package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JOptionPane;

import engine.ARFFReader;
import engine.Logger;
import engine.Project;
import engine.ARFFReader.ProjectPropertyDescriptor;

public class CompareButton extends JButton implements ActionListener{
	ArrayList<ProjectPropertyDescriptor> characteristics;
	ArrayList<Project> oldProjects;
	private StartWindow parent;
	
	public CompareButton(StartWindow parent) {
		super("Compare");
		addActionListener(this);
		setEnabled(false);
		
		this.parent = parent;
	
	}
	
	public boolean setDataSource(String file){
		try {
			this.characteristics = ARFFReader.getProjectDescriptors(file);
			this.oldProjects = ARFFReader.parse(file);
			
			if (this.oldProjects == null || this.characteristics == null || 
				this.oldProjects.size() == 0 || this.characteristics.size() == 0) {
				
				throw new IllegalArgumentException("Unable to read anything important from the file");

			}				
			
			setEnabled(true);
				
			return true;
		} catch (Exception e) {
			Logger.printStackTrace(e);
			JOptionPane.showMessageDialog(null, "Unable to parse the file \"" + file + "\". Perhaps it is empty, non-existing, the program can't read it or it simply doesn't comply with the arff format.");
		}
		return false;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		CompareWindow window = new CompareWindow(oldProjects, characteristics);
		
		window.setLocation(parent.getX() + parent.getWidth(), parent.getY());
	}

}
